package com.hao.wms.mvc;

import com.hao.wms.domain.Role;
import com.hao.wms.query.QueryObject;
import com.hao.wms.service.IPermissionService;
import com.hao.wms.service.IRoleService;
import com.hao.wms.service.ISystemMenuService;
import com.hao.wms.util.RequiredPermission;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import java.io.IOException;

public class RoleAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IRoleService roleService;
	private IPermissionService permissionService;
	private ISystemMenuService systemMenuService;
	
	
	public void setPermissionService(IPermissionService permissionService) {
		this.permissionService = permissionService;
	}
	private Role role = new Role();
	
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	private QueryObject queryObject = new QueryObject();
	
	
	public QueryObject getQueryObject() {
		return queryObject;
	}
	public void setQueryObject(QueryObject queryObject) {
		this.queryObject = queryObject;
	}
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	public void setSystemMenuService(ISystemMenuService systemMenuService) {
		this.systemMenuService = systemMenuService;
	}

	@InputConfig(methodName = "input")
	@RequiredPermission(value="角色列表")
	public String execute() {
		putContext(PAGERESULT, roleService.query(queryObject));
		
		return LIST;
	}
	@RequiredPermission(value="新增/修改角色")
	public String input() {
		putContext("permissions", permissionService.list());
		putContext("menus",systemMenuService.queryChildrenMenus());
		if(role.getId()!=null) {
			role = roleService.get(role.getId());
		}
		return INPUT;
	}
	@RequiredPermission(value="修改角色")
	public String save() {
		try {
			if(role.getId()!=null) {
                roleService.update(role);
                addActionMessage("修改成功");
            }else {
                roleService.save(role);
                addActionMessage("保存成功");
            }
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	public void prepareSave(){
		if(role.getId()!=null) {
			role = roleService.get(role.getId());
			
		}
		role.getPermissions().clear();
		role.getMenus().clear();
	}
	@RequiredPermission(value="删除角色")
	public String delete() throws IOException {
		roleService.delete(role);
		putResponseText("删除成功");
		return NONE;
	}
	
	

}
